package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.module.entity.ApartmentLabel;
import com.atguigu.lease.module.entity.LabelInfo;
import com.atguigu.lease.module.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.ApartmentLabelMapper;
import com.atguigu.lease.web.admin.mapper.LabelInfoMapper;
import com.atguigu.lease.web.admin.service.ApartmentLabelService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ApartmentLabelServiceImpl extends ServiceImpl<ApartmentLabelMapper, ApartmentLabel> implements ApartmentLabelService {
    @Autowired
    private LabelInfoMapper labelInfoMapper;

    @Override
    public List<LabelInfo> getApartmentLabelListByApartmentId(Long id) {
        //先根据apartmentId获取中间关系表
        List<ApartmentLabel> apartmentLabelList = super.list(new LambdaQueryWrapper<ApartmentLabel>().eq(ApartmentLabel::getApartmentId, id));
        if(apartmentLabelList == null || apartmentLabelList.size()<=0){
            return null;
        }
        //因为中间关系表只有id 表示哪些公寓有哪些标签 所以还要查询每个公寓对应标签的详细信息
        //1.可以通过内置api mapper层的selectList（）
        List<LabelInfo> labelInfoList = labelInfoMapper.selectList(new LambdaQueryWrapper<LabelInfo>().eq(LabelInfo::getType, ItemType.APARTMENT).in(LabelInfo::getId, apartmentLabelList.stream().map(ApartmentLabel::getLabelId).collect(Collectors.toList())));


        return labelInfoList;
    }
}
